package team.knowers.qzh.qzhgrame.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import team.knowers.qzh.qzhgrame.common.convention.exception.ClientException;
import team.knowers.qzh.qzhgrame.dao.entity.ChapterDO;
import team.knowers.qzh.qzhgrame.dao.entity.SectionDO;
import team.knowers.qzh.qzhgrame.dao.mapper.ChapterMapper;
import team.knowers.qzh.qzhgrame.dto.req.AddChapterReqDTO;
import team.knowers.qzh.qzhgrame.dto.req.DeleteChapterReqDTO;
import team.knowers.qzh.qzhgrame.dto.req.UpdateChapterReqDTO;
import team.knowers.qzh.qzhgrame.dto.resp.AllChapterRespDTO;
import team.knowers.qzh.qzhgrame.service.ChapterGrameService;

import java.util.ArrayList;
import java.util.List;

import static team.knowers.qzh.qzhgrame.common.web.FourteenDigitRandomGenerator.generateFourteenDigitRandom;

@Service
@AllArgsConstructor
public class ChapterGrameServiceImpl extends ServiceImpl<ChapterMapper, ChapterDO> implements ChapterGrameService {

    @Override
    public int addChapter(AddChapterReqDTO req) {
        LambdaQueryWrapper<ChapterDO> queryWrapper= Wrappers.lambdaQuery(ChapterDO.class)
                .eq(ChapterDO::getName,req.getName())
                .eq(ChapterDO::getOrder,req.getOrder())
                .eq(ChapterDO::getCourseId,req.getCourseId())
                .eq(ChapterDO::getDelFlag,0);
        ChapterDO chapterDO=baseMapper.selectOne(queryWrapper);
        if(chapterDO!=null)
        {
            throw new ClientException("课程单元已存在");
        }
        long a=generateFourteenDigitRandom();
        ChapterDO bean= BeanUtil.toBean(req,ChapterDO.class);
        bean.setId(a);
        return baseMapper.insert(bean);
    }

    @Override
    public int deleteChapter(Long id) {
        LambdaQueryWrapper<ChapterDO> queryWrapper= Wrappers.lambdaQuery(ChapterDO.class)
                .eq(ChapterDO::getId,id)
                .eq(ChapterDO::getDelFlag,0);
        ChapterDO chapterDO=baseMapper.selectOne(queryWrapper);
        chapterDO.setDelFlag(1);
        return baseMapper.update(chapterDO,queryWrapper);
    }

    @Override
    public int updateChapter(UpdateChapterReqDTO req) {
        LambdaQueryWrapper<ChapterDO> queryWrapper=Wrappers.lambdaQuery(ChapterDO.class)
                .eq(ChapterDO::getId,req.getId())
                .eq(ChapterDO::getCourseId,req.getCourseId())
                .eq(ChapterDO::getDelFlag,0);
        ChapterDO chapterDO=baseMapper.selectOne(queryWrapper);
        chapterDO.setOrder(req.getOrder());
        chapterDO.setName(req.getName());
        LambdaUpdateWrapper<ChapterDO> updateWrapper=Wrappers.lambdaUpdate(ChapterDO.class)
                .eq(ChapterDO::getId,req.getId())
                .eq(ChapterDO::getCourseId,req.getCourseId())
                .eq(ChapterDO::getDelFlag,0);
        return baseMapper.update(chapterDO,updateWrapper);
    }

    @Override
    public List<AllChapterRespDTO> showAllChapter(Long id) {
        LambdaQueryWrapper<ChapterDO> queryWrapper=Wrappers.lambdaQuery(ChapterDO.class)
                .eq(ChapterDO::getCourseId,id)
                .eq(ChapterDO::getDelFlag,0)
                .orderByAsc(ChapterDO::getOrder);
        List<ChapterDO> chapterDOS=baseMapper.selectList(queryWrapper);
        List<AllChapterRespDTO> chapterDTOs = new ArrayList<>();
        for (ChapterDO chapterDO :chapterDOS) {
            AllChapterRespDTO chapterDTO = new AllChapterRespDTO();
            chapterDTO.setOrder(chapterDO.getOrder());
            chapterDTO.setName(chapterDO.getName());
            chapterDTO.setId(chapterDO.getId());
            chapterDTOs.add(chapterDTO);
        }

        return chapterDTOs;
    }
}
